package org.clever.mybatis.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.clever.mybatis.dto.QueryPage2;
import org.clever.mybatis.entity.CoreAccessLog;
import org.clever.mybatis.entity.CoreConfig;
import org.clever.mybatis.mapper.CoreAccessLogMapper;
import org.clever.mybatis.mapper.CoreConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 作者： lzw<br/>
 * 创建时间：2018-09-14 18:05 <br/>
 */
@Api(description = "测试")
@RequestMapping("/test")
@RestController
@Slf4j
public class TestController {

    @Autowired
    private CoreAccessLogMapper coreAccessLogMapper;

    @Autowired
    private CoreConfigMapper coreConfigMapper;

    @GetMapping("/query")
    public Object query() {
        List<CoreConfig> list = coreConfigMapper.query("default");
        log.info("List================{}", list.size());
        // 自带分页插件
        Page page = new Page(3, 3);
        list = coreConfigMapper.queryPage(page, "default");
        log.info("Page================{}", list.size());

        list = coreConfigMapper.queryPage(null, "default");
        log.info("Page不分页================{}", list.size());
        return list;
    }

    @GetMapping("/update")
    public Object update() {
        CoreAccessLog coreAccessLog = coreAccessLogMapper.selectById(1);
        log.info("CoreAccessLog================={}", coreAccessLog);

        CoreConfig coreConfig = coreConfigMapper.selectById(1);
        log.info("CoreConfig================={}", coreConfig);

        CoreConfig update = new CoreConfig();
        update.setId(coreConfig.getId());
        update.setUpdateDate(new Date());
        update.setUpdateBy("123");

        coreConfigMapper.updateById(update);
        return coreConfigMapper.selectById(1);
    }

    @GetMapping("/delete")
    public Object delete() {
        return coreConfigMapper.deleteAll();
    }

    @GetMapping("/query2")
    public Object query2() {
        Page page = new Page(3, 3);
        QueryPage2 queryPage2 = new QueryPage2();
        queryPage2.setParamStr("default");
        List<CoreConfig> list = coreConfigMapper.queryPage2(queryPage2, page);
        log.info("Page================{}", list.size());
        return list;
    }
}
